<template>
  <div class="course">
    <main>
      <div class="screening">
        <ul class="ul1">
          <p>课程分类:</p>
          <li :class="{this1:index1==null}" @click="show1()">全部</li>
          <li v-for="(item,index) in courseClassify" :class="{this1:index==index1}" @click="show2(item,index)">{{item.title}}</li>
        </ul>
        <ul class="ul2">
          <p>筛&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;选:</p>
          <li :class="{this2:index2==1}" @click="show3()">默认</li>
          <li :class="{this2:index2==2}" @click="show4()">人气</li>
          <li :class="{this2:index2==3,change1: change1,change2:change2}" class="price" @click="show5()">价格</li>
        </ul>
      </div>
      <div class="course-list">
        <dl v-for="(item,index) in courseList" :key="index">
          <dt>
            <img :src="imgLoad(item.course_img)" @click="routerClick(item.id)"/>
          </dt>
          <dd>
            <div class="name" @click="routerClick(item.id)">
              <p>{{item.title}}</p>
              <p><img src="../assets/toux.svg"/>{{item.popularity}}人已加入学习</p>
            </div>
            <div class="teacher" @click="routerClick(item.id)">
              <p>{{item.teacher}}</p>
              <p>共{{item.course_hour}}课时<span>/{{courseState(item.course_hour,item.complete_numbers)}}</span></p>
            </div>
            <ul>
              <li v-for="(i,z) in item.category" :key="i.id">
                <img  src="../assets/bf.svg"/>
                <p>0{{z+1}} | {{i.title}}</p>
                <span v-if="i.is_free">免费</span>
              </li>
            </ul>
            <div class="other">
              <p>
                <span v-if="item.price_type " class="discount">{{item.price_type}}</span>
                <span class="present-price">￥{{item.now_price}}元</span>
                <span class="original-price">原价：{{item.cost_price}}元</span>
              </p>
              <button>立即购买</button>
            </div>
          </dd>
        </dl>
      </div>
    </main>
  </div>
</template>

<script>
  export default {
    name: "Free",
    data(){
      return{
        courseClassify:null,
        courseList:null,
        condition1:0,
        condition2:'',
        index1:null,
        index2:1,
        price:true,
        change1:false,
        change2:false
      }
    },
    computed:{
    },
    mounted(){
      this.getClassify();
      this.filterClassify(this.condition1,this.condition2);
    },
    methods:{
      getClassify(){
        var _this = this;
        this.$axios.request({
          url:'http://127.0.0.1:8010/api/v1/courses/classifyViews/list/',
          method:'get',
          headers:{
            'Content-Type':'application/json',
          }
        }).then(function (msg) {
          _this.courseClassify = msg.data.data

        }).catch(function (msg) {
          console.log(msg.data)
        })
      },
      filterClassify(sub_category,ordering){
        var _this = this;
        this.$axios.request({
          url:'http://127.0.0.1:8010/api/v1/courses/?sub_category='+sub_category+'&ordering='+ordering
        }).then(function (msg) {
          if(msg.data.code === 1){
            _this.courseList = msg.data.data
          }else{
            console.log(msg.data.error)
          }
        }).catch(function (msg) {
          console.log(msg)
        })
      },
      routerClick(pk){
        this.$router.push({name:'CourseDetail',params:{id:pk}})
      },
      show1(){
        this.index1 = null;
        this.condition1 = 0;
        this.filterClassify(this.condition1,this.condition2)

      },
      show2(item,index){
        this.index1 = index;
        this.condition1 = index+1;
        this.filterClassify(this.condition1,this.condition2)
      },
      show3(){
        this.index2 = 1;
        this.condition2 = '';
        this.filterClassify(this.condition1,this.condition2)
      },
      show4(){
        this.index2 = 2;
        this.condition2 = '-popularity';
        this.filterClassify(this.condition1,this.condition2)
      },
      imgLoad(name){
        return require('../assets/'+name)
      },
      courseState(x,y){
        if(parseInt(x)<=parseInt(y)){
          return "更新完成"
        }else{
          return "已跟新"+y+"课时"
        }
      },
      show5(){
        this.index2 = 3;
        this.change1 = !this.change1;
        if(this.change1){
          this.change2 = false;
          this.condition2 = "price";
        }else{
          this.change2 = true;
          this.condition2 = "-price";
        }
        this.filterClassify(this.condition1,this.condition2)
      }
    },

  }
</script>

<style scoped>
  .course{
    width: 100%;
    height: auto;
    display: flex;
    align-items:flex-start;
  }
  .course>main{
    width: 1100px;
    margin: 0 auto;
    padding-top: 35px;
  }
  .screening{
    width: 100%;
    height: auto;
    margin-bottom: 35px;
    padding: 25px 30px 25px 20px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 4px 0 #f0f0f0;
  }
  .screening>ul{
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    font-size: 16px;
    color: #888;
    letter-spacing: .36px;
  }
  .screening>ul>li{
    list-style: none;
    padding: 6px 16px;
    line-height: 16px;
    margin-left: 14px;
    position: relative;
    transition: all .3s ease;
    cursor: pointer;
  }
  .ul1{
    border-bottom: 1px solid #333;
    border-bottom-color: rgba(51,51,51,.05);
    padding-bottom: 18px;
    margin-bottom: 17px;
  }
  .ul1>p{
    margin: 0;
  }

  .ul2>p{
    margin: 0;
  }
  .this1{
    color: #ffc210;
    border: 1px solid #ffc210!important;
    border-radius: 30px;
  }
  .this2{
    color:#ffc210 ;
  }
  .course-list{
    width: 100%;
  }
  .course-list>dl{
    width: 100%;
    height: auto;
    background: #fff;
    padding: 20px 30px 20px 20px;
    display: -ms-flexbox;
    display: flex;
    margin-bottom: 35px;
    border-radius: 2px;
    cursor: pointer;
    box-shadow: 2px 3px 16px rgba(0,0,0,.1);
    transition: all .2s ease;
    position: relative;
  }
  .course-list>dl>dt{
    width: 423px;
    height: 210px;
    margin-right: 30px;
  }
  .course-list>dl>dt>img{
    width: 423px;
    height: 210px;
  }
  .price::before{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-top-color: #d8d8d8;
    position: absolute;
    right: 0;
    bottom: 2.5px;
  }
  .price::after{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-bottom-color: #d8d8d8;
    position: absolute;
    right: 0;
    top: 2.5px;
  }
  .change1::before{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-top-color: #d8d8d8;
    position: absolute;
    right: 0;
    bottom: 2.5px;
  }
  .change1::after{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-bottom-color: #ffc210;
    position: absolute;
    right: 0;
    top: 2.5px;
  }
  .change2::before{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-top-color: #ffc210;
    position: absolute;
    right: 0;
    bottom: 2.5px;
  }
  .change2::after{
    content: "";
    width: 0;
    border: 5px solid transparent;
    border-bottom-color: #d8d8d8;
    position: absolute;
    right: 0;
    top: 2.5px;
  }
  .name{
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    margin-bottom: 8px;
  }
  .name>p{
    display: flex;
  }
  .name>p:first-child{
    font-size: 26px;
    color: #333;
  }
  .name>p:last-child{
    font-size: 14px;
    color: #9b9b9b;
  }
  .teacher{
    display: flex;
    -ms-flex-align: end;
    align-items: flex-end;
    -ms-flex-pack: justify;
    justify-content: space-between;
    font-size: 14px;
    color: #9b9b9b;
    margin-bottom: 14px;
    padding-bottom: 14px;
    border-bottom: 1px solid #333;
    border-bottom-color: rgba(51,51,51,.05);
  }
  .course-list dl dd ul{
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
  }
  .course-list >dl >dd >ul{
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
  }
  .course-list >dl >dd >ul>li{
    width: 49%;
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #666;
    margin-bottom: 15px;
    cursor: pointer;
  }
  .course-list >dl >dd >ul>li>span{
    width: 34px;
    height: 20px;
    color: #fd7b4d;
    margin-left: 10px;
    border: 1px solid #fd7b4d;
    border-radius: 2px;
    text-align: center;
    font-size: 13px;
    white-space: nowrap;
  }
  .course-list >dl >dd >ul>li:hover{
    color:#ffc210 ;
  }
  .course-list >dl >dd >ul>li>p{
    max-width: 227px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
  .course-list >dl >dd >ul>li>img{
    width: 16px;
    height: 16px;
    margin-right: 6px;
  }
  .discount{
    padding: 6px 10px;
    font-size: 16px;
    color: #fff;
    text-align: center;
    margin-right: 8px;
    background: #fa6240;
    border: 1px solid #fa6240;
    border-radius: 10px 0 10px 0;
  }
  .present-price{
    font-size: 24px;
    color: #fa6240;
  }
  .original-price{
    text-decoration: line-through;
    font-size: 14px;
    color: #9b9b9b;
    margin-left: 10px;
  }
  .other{
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 7px;
  }
  .other>button{
    width: 120px;
    height: 38px;
    background: transparent;
    color: #fa6240;
    font-size: 16px;
    border: 1px solid #fd7b4d;
    border-radius: 3px;
    transition: all .2s ease-in-out;
    cursor: pointer;
  }
  .other>button:hover{
    background-color: #ffc210;
    border: 1px solid #ffc210;
    color: #fff;
  }
</style>
